package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 套餐分页功能
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        // 开始分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        // 查询套餐列表
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        // 返回分页结果
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void saveWithFlavor(SetmealDTO setmealDTO) {

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        //向套餐表里加数据
        setmealMapper.insert(setmeal);

        //获取生产的套餐id
        Long  setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setDishId(setmealId));

        //保持套餐和菜品的关系
        setmealDishMapper.insertBatch(setmealDishes);


    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    public void deleteBatch(List<Long> ids) {
        //判断套餐是否能删除，判断当前的套餐是否起售
        for (Long id : ids) {
              Setmeal setmeal=  setmealMapper.getById(id);

              if (setmeal.getStatus().equals(StatusConstant.ENABLE)){
                  //判断当前套餐处于起售中不能删除
                  throw  new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);

              }
        }

        //删除套餐表中的数据
        for (Long id : ids) {

            //删除套餐表中的套餐数据
            setmealMapper.deleteById(id);

            //删除套餐菜品关系表中的数据
            setmealDishMapper.deleteBySetmealId(id);
        }

    }

    /**
     * 更新状态
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //起售套餐时，判断套餐内是有停售菜品，有停售菜品提示套餐内包含未起售菜品，无法起售
        if (status.equals(StatusConstant.ENABLE)){
           List<Dish> dishList= dishMapper.getBySetmealId(id);

           if (!CollectionUtils.isEmpty(dishList)){
               for (Dish dish : dishList) {
                   if (dish.getStatus().equals(StatusConstant.DISABLE)){
                       throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                   }
               }
           }

        }
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
    }

    /**
     * 根据id查询套餐和套餐菜品关系
     * @param id
     * @return
     */
    @Override
    public SetmealVO getByIdWithDish(Long id) {
       SetmealVO setmealVO=  setmealMapper.getByIdWithDish(id);
       return setmealVO;
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }
    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {

        return setmealMapper.getDishItemBySetmealId(id);
    }

}
